package com.wiser.ohoscard;

import com.wiser.ohoscard.db.Form;
import com.wiser.ohoscard.db.FormDatabase;
import com.wiser.ohoscard.slice.MainAbilitySlice;
import com.wiser.ohoscard.utils.ComponentProviderUtils;
import com.wiser.ohoscard.utils.DatabaseUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ComponentProvider;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 必须xml中父布局中添加 ohos:remote="true" 属性 才会显示服务卡片
 */
public class MainAbility extends Ability {

    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP,0,"服务卡片");

    private static final int DEFAULT_DIMENSION_2X2 = 2;

    private static final int DEFAULT_DIMENSION_2X4 = 3;

    private static final String EMPTY_STRING = "";

    private static final int INVALID_FORM_ID = -1;

    private long formId;

    private ProviderFormInfo formInfo;

    private DatabaseHelper helper = new DatabaseHelper(this);

    private OrmContext connect;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class);
        Intent intentService = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(TimerAbility.class.getName())
                .build();
        intentService.setOperation(operation);
        startAbility(intentService);
    }

    @Override
    protected ProviderFormInfo onCreateForm(Intent intent) {
        if (intent == null) {
            return new ProviderFormInfo();
        }
        // 获取卡片id
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) {
            formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        } else {
            return new ProviderFormInfo();
        }
        // 获取卡片名称
        String formName = EMPTY_STRING;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) {
            formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
        }
        // 获取卡片规格
        int dimension = DEFAULT_DIMENSION_2X2;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) {
            dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
        }
        int layoutId = ResourceTable.Layout_form_ability_layout_2_2;
        if (dimension == DEFAULT_DIMENSION_2X4) {
            layoutId = ResourceTable.Layout_form_ability_layout_2_4;
        }
        formInfo = new ProviderFormInfo(layoutId, this);
        // 存储卡片信息 否则会出现弹出服务卡片时刷新延迟
        Form form = new Form(formId, formName, dimension);
        ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(form, this);
        formInfo.mergeActions(componentProvider);
        if (connect == null) {
            connect =
                    helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class);
        }
        try {
            DatabaseUtils.insertForm(form, connect);
        } catch (Exception e) {
            DatabaseUtils.deleteFormData(form.getFormId(), connect);
        }
        return formInfo;
    }

    @Override
    protected void onDeleteForm(long formId) {
        super.onDeleteForm(formId);
        // 删除数据库中的卡片信息
        DatabaseUtils.deleteFormData(formId, connect);
    }
}
